package com.ghr.ai.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequiredArgsConstructor
@RequestMapping("/ai")
public class ChatController {

    private final ChatClient chatClient;

    /**
     * 阻塞式
     * @param prompt
     * @return
     */
    @GetMapping("/chat")
    public String chat(@RequestParam("prompt") String prompt) {
        return chatClient.prompt(prompt)
                .user(prompt)
                .call()
                .content();
    }

    @GetMapping(value = "/chatFlux", produces = "text/html;charset=UTF-8")
    public Flux<String> chatWithStream(@RequestParam("prompt") String prompt) {
        return chatClient.prompt(prompt)
                .user(prompt)
                .stream()
                .content();
    }

}
